using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data;
using System.IO;
using MySql.Data.MySqlClient;
namespace System {
	public class MAL {
		public string lastSql="";
		public static void Csv(DataTable tab,string FileName) {
			using(StreamWriter sw=File.CreateText(FileName))
			{
				int cntc=0;
				string[] cols=new string[tab.Columns.Count];
				if(tab.Rows.Count>0)
				{
					foreach(DataColumn col in tab.Columns)
						cols[cntc++]=col.ColumnName;
					sw.WriteLine(string.Join(",",cols));
					foreach(DataRow row in tab.Rows)
					{
						string[] arr=new string[row.ItemArray.Length];
						int cnt=0;
						foreach(object obj in row.ItemArray)
							arr[cnt++]=obj.ToString().Trim();
						sw.WriteLine(string.Join(",",arr));
					}
				}
			}
		}

		public static string CsvColumn(DataTable tab,string colName,bool quote) {
			StringBuilder sb=new StringBuilder();
			List<string> vals=new List<string>(tab.Rows.Count);
			foreach(DataRow row in tab.Rows)
				//if(!vals.Contains(row[colName].ToString()))
				vals.Add(row[colName].ToString());
			if(quote)
				return string.Concat("'",string.Join("','",vals.ToArray()),"'");
			else
				return string.Join(",",vals.ToArray());
		}
		string _cs;
		public MAL() : this("server=localhost;database=test;uid=sysuser;pwd=syspass") { }
		public MAL(params string[] args) {
			if(args.Length==1)
				_cs=args[0];
			else if(args.Length==4)
				_cs=string.Format("server={0};database={1};uid={2};pwd={3}",args);
			else
				throw new Exception();
		}
		public DataTable Query(string sql,params object[] args) {
			string qry=string.Format(sql,args);
			lastSql=qry;
			DataTable ret=new DataTable();
			using(MySqlDataAdapter adp=new MySqlDataAdapter(qry,_cs))
			{
				adp.SelectCommand.CommandTimeout=0;
				adp.Fill(ret);
			}
			return ret;
		}
		public DataSet QuerySet(string sql,params object[] args) {
			string qry=string.Format(sql,args);
			DataSet ret=new DataSet();
			using(MySqlDataAdapter adp=new MySqlDataAdapter(qry,_cs))
			{
				adp.SelectCommand.CommandTimeout=180;
				adp.Fill(ret);
			}
			return ret;
		}
		public static string LastQry;

		public int NonQry(string sql,params object[] args) {


			string qry=sql;
			if(args.Length!=0)
			{
				//for(int i=0;i<args.Length;i++)
				//  args[i]=args[i].ToString().Replace("'","''");
				qry=string.Format(sql,args);
			}
			LastQry=qry;
			int ret=-1;
			using(MySqlConnection con=new MySqlConnection(_cs))
			using(MySqlCommand cmd=new MySqlCommand(qry,con))
			{
				cmd.CommandTimeout=100;
				con.Open();
				ret=cmd.ExecuteNonQuery();
				con.Close();
			}
			//Console.WriteLine(ret);
			return ret;
		}

		public object Scalar(string sql,params object[] args) {
			//System.Diagnostics.Stopwatch sw=System.Diagnostics.Stopwatch.StartNew();
			string qry=string.Format(sql,args);
			lastSql=qry;
			object ret=null;
			using(MySqlConnection con=new MySqlConnection(_cs))
			using(MySqlCommand cmd=new MySqlCommand(qry,con))
			{
				//Console.WriteLine(con.ConnectionTimeout);
				con.Open();
				ret=cmd.ExecuteScalar();
				con.Close();
			}
			//Console.WriteLine("------"+sw.Elapsed);
			return ret;
		}
	}
}
